#!/usr/bin/env python
# encoding: utf-8
"""
@summary: 将hdfs上的文件处理成车型数据
@author: hongxingfan
@since: 2014年8月7日    下午4:30:48
"""
from os.path import os
import datetime
import MySQLdb
import sys
class Deal:

    modelIdToInfo = {}

    def __init__(self, host, uname, passwd , dbname):
        self.host = host
        self.uname = uname
        self.passwd = passwd
        self.dbname = dbname
        self.DBToHash()
        
    # 将数据装载到hash结构中
    def DBToHash(self):
        try:
            conn = MySQLdb.connect(host = self.host, user = self.uname, passwd = self.passwd, db = self.dbname, charset = "utf8") 
            cursor = conn.cursor()
            sql_model = "SELECT model.id AS modelId,corp.ZH_NAME AS corpName,model.zh_name AS modelName FROM model LEFT JOIN corp ON model.CORP_ID=corp.ID"
            # 执行model查询
            cursor.execute(sql_model)
            for (modelId, corpName, modelName) in cursor.fetchall():
                self.modelIdToInfo[modelId] = corpName + "\t" + modelName
            # 执行trim查询
        except MySQLdb.Error, e:
            print("Error Code is:%s; Error Message is:%s" % (e.args[0], e.args[1]))
            sys.exit()
        finally:
            cursor.close()
            conn.close()
    # 从hdfs上拉取文件,并转化成中文
    def getDataFromHDFS(self, yesterday):
        list = []
        try:
            hdfs = os.popen("hadoop dfs -text /user/autolog/tmp/fan/ModelOfPopularity_" + yesterday + "/part*")
            # hdfs = os.popen("hadoop dfs -text /user/autolog/tmp/fan/ModelOfPopularity_20140806/part-00000")
            corp_model = ""
            for line in hdfs.readlines():
                line = line.strip()
                cols = line.split("\t")
                typeId = int(cols[0])  # 需要转化成整形,否则在hash结构中取不到数据
                pv = cols[1]
                uv = cols[2]
                # 是trim的情况，先转化成model
                corp_model = self.modelIdToInfo.get(typeId, str(typeId))
                entity = Entity(corp_model.encode("utf-8"), pv, uv)  # 解决乱码问题
                list.append(entity)
        except:
            print("hdfs command has error...")
        finally:
            hdfs.close()
        list.sort(lambda x, y: cmp(int(y.pv), int(x.pv)))  # 列表排序
        return list

# 实体类    
class Entity(object):
    def __init__(self, corp_model, pv, uv):
        self.corp_model = corp_model
        self.pv = pv
        self.uv = uv
    def __unicode__(self):
        print("%s\t%s\t%s\t%s" % (self.corp_model, self.pv, self.uv))

# 测试执行
if __name__ == "__main__":
    deal = Deal(host = "10.10.77.6", uname = "wanjiang", passwd = "wanjiang0310", dbname = "auto_warehouse")
    
    now = datetime.datetime.now()
    yesterday = now + datetime.timedelta(days = -1)
    yesterday = yesterday.strftime('%Y%m%d')
    
    for entity in deal.getDataFromHDFS(yesterday):
        print("%s\t%s\t%s" % (entity.corp_model, entity.pv, entity.uv))
